package com.gem.controller;

import com.gem.domain.User;
import com.gem.service.UserSystemService;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Bravea❤ ·勇
 * Description: 人员维护controller
 * create
 * Modified By:
 */
@Controller
@RequestMapping("/user")
public class UserSystemController {

    @Autowired
    private JSONResultUtil result;

    @Autowired
    private UserSystemService userSystemService;

    @RequestMapping("list")
    public String list(Model model, HttpSession session,
                       @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            MessageUtil.returnInfoPage("3", "/starter", "用户未登录", model);
            return "message";
        }
        PageInfo<User> pageBean = null;
        if (user.getUid() == 1) {
            pageBean = userSystemService.findAllUserPage(currentPage, pageSize);
        } /*else if (user.getUid() == 2) {
            pageBean = userSystemService.findUserPageWidtM(currentPage, pageSize);
        } else if (user.getUid() == 4) {
            pageBean = userSystemService.findUserPageWidtHrc(currentPage, pageSize);
        } else if (user.getUid() == 5) {
            pageBean = userSystemService.findUserPageWidtCompany(currentPage, pageSize);
        } else if (user.getUid() == 6) {
            pageBean = userSystemService.findUserPageWidtJober(currentPage, pageSize);
        }*/ else {
            MessageUtil.returnInfoPage("3", "/starter", "您没有访问权限！", model);
            return "message";
        }
        if (currentPage > pageBean.getPages()) {
            currentPage = pageBean.getPages();
            pageBean = userSystemService.findAllUserPage(currentPage, pageSize);
        }
        if (pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0) {
            List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
            model.addAttribute("pageValue", pageValue);
        } else {
            model.addAttribute("info", "没有用户数据请先添加！");
        }
        model.addAttribute("pageBean", pageBean);
        return "system/members";
    }

    @RequestMapping("/addUI")
    public String addUI(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage, String searchUrl,
                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, Model model) {
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("searchUrl", searchUrl);
        return "system/member_insert";
    }

    @RequestMapping("/add")
    public String add(User user, Model model, String searchUrl,
                      @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        boolean isAddSuccess = userSystemService.addUser(user);
        if (isAddSuccess) {
            if (searchUrl != null && !searchUrl.trim().equals("")) {
                MessageUtil.returnInfoPage("3", searchUrl + "&currentPage=" + currentPage + "&pageSize=" + pageSize, "添加成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/user/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加成功！", model);
            }
        } else {
            if (searchUrl != null && !searchUrl.trim().equals("")) {
                MessageUtil.returnInfoPage("3", searchUrl + "&currentPage=" + currentPage + "&pageSize=" + pageSize, "添加失败，请检查！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/user/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加失败，请检查！", model);
            }
        }
        return "message";
    }

    @RequestMapping("/modifyUI")
    public String modifyUI(Integer uid,Model model,String searchUrl,
                           @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        if(uid == null || uid < 0){
            if (searchUrl == null){
                MessageUtil.returnInfoPage("3","/user/list?currentPage="+currentPage+"&pageSize="+pageSize,"传递参数为空",model);
            }else {
                MessageUtil.returnInfoPage("3",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"传递参数为空",model);
            }
            return "message";
        }
        User user = userSystemService.findUserById(uid);
        if (user == null){
            if (searchUrl == null) {
                MessageUtil.returnInfoPage("3","/user/list?currentPage="+currentPage+"&pageSize="+pageSize,"没有该员工信息",model);
            }else {
                MessageUtil.returnInfoPage("3",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"没有该员工信息",model);
            }
            return "message";
        }
        model.addAttribute("searchUrl",searchUrl);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("user",user);
        return "system/member_update";
    }

    @RequestMapping("/modify")
    public String modify(User user,Model model,String searchUrl,
                         @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        boolean isModifySuccess = userSystemService.modifyUser(user);
        if(isModifySuccess){
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("3",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"修改成功！",model);
            }else {
                MessageUtil.returnInfoPage("3", "/user/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "修改成功！", model);
            }
        }else {
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("3",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"修改失败，请检查！",model);
            }else {
                MessageUtil.returnInfoPage("3", "/user/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "修改失败，请检查！", model);
            }
        }
        return "message";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil delete(Integer[] checks){
        try {
            if (checks != null && checks.length>0) {
                if (userSystemService.deleteUserById(checks)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("删除失败！");
                }
            } else {
                result.setId(-1);
                result.setData("没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    @RequestMapping("/search")
    public String search(Model model,String personname,
                         @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        PageInfo<User> pageBean = userSystemService.findUserWithPageByName(currentPage,pageSize,personname);
        if (currentPage > pageBean.getPages()){
            currentPage = pageBean.getPages();
            pageBean = userSystemService.findUserWithPageByName(currentPage,pageSize,personname);
        }
        if(pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0){
            List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(),currentPage);
            model.addAttribute("pageValue",pageValue);
        }else {
            model.addAttribute("info","没有员工信息，请先添加！");
        }
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("personname",personname);
        model.addAttribute("searchUrl","/user/search?personname=" + personname);
        return "system/members";
    }

}
